package com.shaoshaohuo.app.ui.ec;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.SupplyOrderListAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.SupplyOrder;
import com.shaoshaohuo.app.entity.SupplyOrderEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yusonglin
 * @类 说 明:
 * @version 1.0
 * @创建时间：2015年8月31日 上午10:19:42
 * 
 */
public class SupplyOrderActivity extends BaseActivity implements OnClickListener, IXListViewListener {

	public static final int STATUS_DAIFUKUAN = 1;
	public static final int STATUS_DAIFAHUO = 2;
	public static final int STATUS_DAISHOUHUO = 3;
	public static final int STATUS_FINISH = 4;

	private TopbarView mTopbarView;

	private TextView mDaifukuanText;
	private TextView mDaifahuoText;
	private TextView mYifahuoText;
	private TextView mYiwanchengText;

	private XListView mListView;
	private SupplyOrderListAdapter<SupplyOrder> mAdapter;

	private List<SupplyOrder> mDataList = new ArrayList<SupplyOrder>();
	private int offset = 10;
	private String action = ListViewAction.NEW2;
	private String cursor = "0";
	private int status = STATUS_DAIFUKUAN;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(new TopbarView(this));
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				setContentView(R.layout.activity_supply_order);
				initView();
				setUpView();
				// getOrderList(true);
			}
		}, 200);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mDaifukuanText = (TextView) this.findViewById(R.id.textview_daifukuan);
		mDaifahuoText = (TextView) this.findViewById(R.id.textview_daifahuo);
		mYifahuoText = (TextView) this.findViewById(R.id.textview_yifahuo);
		mYiwanchengText = (TextView) this.findViewById(R.id.textview_yiwancheng);

		mListView = (XListView) this.findViewById(R.id.listview_data_list);

	}

	private void setUpView() {
		mTopbarView.setCenterText("供应订单");
		mTopbarView.setLeftView(true, true);

		mDaifukuanText.setOnClickListener(this);
		mDaifahuoText.setOnClickListener(this);
		mYifahuoText.setOnClickListener(this);
		mYiwanchengText.setOnClickListener(this);

		onClick(mDaifukuanText);

		// List<String> list = new ArrayList<>();
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		//
		// SupplyOrderListAdapter<String> adapter = new
		// SupplyOrderListAdapter<>(this, list, false);
		// mListView.setAdapter(adapter);

		setListView();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.textview_daifukuan:
			if (mDaifukuanText.isSelected()) {
				return;
			}
			mDaifukuanText.setSelected(true);
			mDaifahuoText.setSelected(false);
			mYifahuoText.setSelected(false);
			mYiwanchengText.setSelected(false);
			changeStatus(STATUS_DAIFUKUAN);
			break;
		case R.id.textview_daifahuo:
			if (mDaifahuoText.isSelected()) {
				return;
			}
			mDaifukuanText.setSelected(false);
			mDaifahuoText.setSelected(true);
			mYifahuoText.setSelected(false);
			mYiwanchengText.setSelected(false);
			changeStatus(STATUS_DAIFAHUO);
			break;
		case R.id.textview_yifahuo:
			if (mYifahuoText.isSelected()) {
				return;
			}
			mDaifukuanText.setSelected(false);
			mDaifahuoText.setSelected(false);
			mYifahuoText.setSelected(true);
			mYiwanchengText.setSelected(false);
			changeStatus(STATUS_DAISHOUHUO);
			break;
		case R.id.textview_yiwancheng:
			if (mYiwanchengText.isSelected()) {
				return;
			}
			mDaifukuanText.setSelected(false);
			mDaifahuoText.setSelected(false);
			mYifahuoText.setSelected(false);
			mYiwanchengText.setSelected(true);
			changeStatus(STATUS_FINISH);
			break;
		}
	}

	private void getOrderList(final int state, boolean isLoading) {
		if (isLoading) {
			startLoadingDialog();
		}

		RequestService.getInstance().getSupplyOrderList(this, action, cursor, offset + "", status + "", SupplyOrderEntity.class,
				new RequestListener() {

					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						dismissLoadingDialog();
						if (resultData.isOk()) {
							if (state == status) {

								SupplyOrderEntity entity = (SupplyOrderEntity) resultData;
								List<SupplyOrder> list = entity.getData().getList();
								mDataList.addAll(list);

								if ((mDataList.size() < offset)
										|| (action.equals(ListViewAction.HISTORY2) && list.size() < offset)) {
									// mListView.setPullLoadEnable(false);
									mListView.endLoadingMore("没有更多了");
								}
								setAdapter(mDataList);
							}

						} else {
							showToast(resultData.getMsg());
						}
						onLoad();
					}

					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
						dismissLoadingDialog();
						showToast(msg);
						onLoad();
					}
				});
	}

	protected void setAdapter(List<SupplyOrder> mDataList2) {
		if (mAdapter == null) {
			mAdapter = new SupplyOrderListAdapter<SupplyOrder>(this, mDataList2, false);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.setData(mDataList2);
		}
	}

	private void setListView() {
		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener(this);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				SupplyOrder order = (SupplyOrder) parent.getItemAtPosition(position);
				Intent intent = new Intent(SupplyOrderActivity.this, SupplyOrderDetailActivity.class);
				intent.putExtra(ExtraName.orderid, order.getOrderid());
				startActivity(intent);
			}
		});
	}

	private void changeStatus(int s) {
	    startLoadingDialog();
		mDataList.clear();
		status = s;
		onRefresh();
	}

	@Override
	public void onRefresh() {
//		if (mDataList.size() > 0) {
//			cursor = mDataList.get(0).getCursor();
//		} else {
			cursor = "0";
//		}
			mDataList.clear();
		action = ListViewAction.NEW2;
		mListView.setPullLoadEnable(true);
		getOrderList(status,false);
	}

	@Override
	public void onLoadMore() {
		if (mDataList.size() > 0) {
			cursor = mDataList.get(mDataList.size() - 1).getCursor();
		} else {
			cursor = "0";
		}
		action = ListViewAction.HISTORY2;
		getOrderList(status,false);
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		// mListView.setRefreshTime(StringHelper.formatDate(System.currentTimeMillis()+""));
	}

}
